const pool = require('./db.js')
class pestdiseaseDao{
  // 根据农田的id获取病虫害的数据
  async getPestData(id){
    try{
      let result = await pool.execute('select * from pestdiseasedata where farmland_id=?',[id])
      return result
    }catch(err){
      console.log("出错："+err)
    }
  }
  // 添加病虫害数据
  async addPestData(farmland_id,type,severity,area,time){
    try{
      let result = pool.execute('insert pestdiseasedata(farmland_id,pest_type,severity,affected_area,timestamp) values(?,?,?,?,?) ',[farmland_id,type,severity,area,time])
      return result
    }catch(err){
      console.log("出错："+err)
    }
  }
  // 修改病虫害数据
  async modifyPestData(id,farmland_id,type,severity,area,time){
    try{
      let result = pool.execute('update pestdiseasedata set farmland_id=?,pest_type=?,severity=?,affected_area=?,timestamp=? where pest_id=?',[farmland_id,type,severity,area,time,id])
      return result
    }catch(err){
      console.log("出错："+err)
    }
  }
  // 删除病虫害数据
  async deletePestData(id){
    try{
      let result = pool.execute('delete from pestdiseasedata where pest_id=?',[id])
      return result
    }catch(err){
      console.log("出错："+err)
    }
  }
}

module.exports = new pestdiseaseDao()